#!/bin/bash
. /opt/hygon/common.sh

os_release=$(eval get_os_release)

if [ $os_release == "centos" ];then
	source /opt/rh/devtoolset-8/enable
fi

patch  -p1</opt/hygon/csv/csv2-kernel.patch
yes "" | make olddefconfig
./scripts/config --enable CONFIG_CRYPTO_DEV_CCP
./scripts/config --enable CONFIG_AMD_MEM_ENCRYPT
./scripts/config --enable CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT
./scripts/config --enable CONFIG_XEN_PV
./scripts/config --disable CONFIG_DEBUG_INFO
make olddefconfig

mkdir -p /opt/hygon/csv/kernel
rm -rf /opt/hygon/csv/kernel/*

case $os_release in
     "anolis")
	     rm -rf ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
	     make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv2" binrpm-pkg
	     rpm -ivh --force --nodeps ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
	     mv ~/rpmbuild/RPMS/x86_64/kernel-*.rpm /opt/hygon/csv/kernel/
	     sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 mem_encrypt=on kvm-amd.sev=1"!' /etc/default/grub
	     grub2-mkconfig -o /boot/efi/EFI/anolis/grub.cfg
             ;;
     "kylin")
	     scripts/config --disable SYSTEM_TRUSTED_KEYS
	     scripts/config --disable SYSTEM_REVOCATION_KEYS
	     make olddefconfig
	     rm -rf ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
	     make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv2" binrpm-pkg
	     rpm -ivh --force --nodeps ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
	     mv ~/rpmbuild/RPMS/x86_64/kernel-*.rpm /opt/hygon/csv/kernel/
	     grubby --set-default=/boot/vmlinuz-5.17.0-csv2
             ;;

     "centos")
	     rm -rf ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
	     make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv2" binrpm-pkg
	     rpm -ivh --force --nodeps ~/rpmbuild/RPMS/x86_64/kernel-*.rpm
	     mv ~/rpmbuild/RPMS/x86_64/kernel-*.rpm /opt/hygon/csv/kernel/
	     sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 mem_encrypt=on kvm-amd.sev=1"!' /etc/default/grub
	     grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
             ;;
     "ubuntu")
	     rm -rf ../linux-*-csv*.deb
	     make -j$(getconf _NPROCESSORS_ONLN) LOCALVERSION="-csv2" bindeb-pkg
	     dpkg -i ../linux-*-csv*.deb
	     sed -i 's!\(^GRUB_CMDLINE_LINUX=.*\)"$!\1 mem_encrypt=on kvm-amd.sev=1"!' /etc/default/grub
	     update-grub
	     mv ../linux-*-csv*.deb  /opt/hygon/csv/kernel/

             ;;
     esac

cd /opt/hygon/csv/
tar czvf kernel.tgz kernel

